package cz.muni.fi.pa165.sportclubmanager.client.modules.ageCategory;

import cz.muni.fi.pa165.sportclubmanager.client.modules.player.PlayerForm;
import cz.muni.fi.pa165.sportclubmanager.service.agecategory.AgeCategoryDTO;
import cz.muni.fi.pa165.sportclubmanager.service.agecategory.AgeCategoryService;
import cz.muni.fi.pa165.sportclubmanager.service.agecategory.AgeCategoryWebService;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;

/**
 *
 * @author Greegus
 */
public class DeleteAgeCategoryDialog extends javax.swing.JFrame {

    /**
     * Creates new form DeleteAgeCategoryDialog
     */
    private AgeCategoryDTO ageCategory;
    private JComponent parent;

    public DeleteAgeCategoryDialog(AgeCategoryDTO ageCategory, JComponent parent) {
        this.ageCategory = ageCategory;
        this.parent = parent;

        initComponents();
        jProgressBar1.setVisible(false);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private void submitDelete() {
        SubmimtDeleteWorker worker = new SubmimtDeleteWorker();
        jProgressBar1.setVisible(true);
        jProgressBar1.setIndeterminate(true);
        cancelButton.setEnabled(false);
        submitButton.setEnabled(false);
        worker.execute();
    }

    public class SubmimtDeleteWorker extends SwingWorker<Integer, Integer> {

        @Override
        protected Integer doInBackground() throws Exception {
            AgeCategoryService ageCategoryService = new AgeCategoryWebService().getAgeCategoryServiceImplPort();

            try {
                ageCategoryService.remove(ageCategory);
                ((AgeCategoriesListPanel) parent).reloadAgeCategories();

            } catch (Exception e) {
                JOptionPane.showMessageDialog(DeleteAgeCategoryDialog.this,
                        "Error deleting age category",
                        "Error",
                        JOptionPane.ERROR_MESSAGE);
                return 1;
            }
            return 0;
        }

        @Override
        protected void done() {
            super.done();
            jProgressBar1.setVisible(false);
            jProgressBar1.setIndeterminate(false);
            cancelButton.setEnabled(true);
            submitButton.setEnabled(true);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        submitButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        jProgressBar1 = new javax.swing.JProgressBar();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Delete age category");
        setResizable(false);

        jLabel1.setText("<html>Do you really want to remove selected age category? <br>This change wil be permanent.</html>");
        jLabel1.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        jLabel1.setVerticalTextPosition(javax.swing.SwingConstants.TOP);

        submitButton.setText("Yes");
        submitButton.setMinimumSize(new java.awt.Dimension(70, 0));
        submitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SubmitActionPerformed(evt);
            }
        });

        cancelButton.setText("No");
        cancelButton.setMinimumSize(new java.awt.Dimension(80, 0));
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(46, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 344, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(submitButton, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(submitButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void SubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SubmitActionPerformed
        submitDelete();
        dispose();
    }//GEN-LAST:event_SubmitActionPerformed

    private void CancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelActionPerformed
        dispose();
    }//GEN-LAST:event_CancelActionPerformed
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JProgressBar jProgressBar1;
    private javax.swing.JButton submitButton;
    // End of variables declaration//GEN-END:variables
}
